require("glon")

--Retreaving
function sql_value_stats( play, Stat )
	steamID = play:SteamID()
	unique_id = sql.QueryValue("SELECT unique_id FROM player_info WHERE unique_id = '"..steamID.."'")
	local value = sql.QueryValue("SELECT '"..Stat.."' FROM player_info WHERE unique_id = '"..steamID.."'")
	play:SetNWString("unique_id", unique_id)
	ply:ChatPrint( "Retreaved" )
	return value
end
 
function sql_value_skills( play, skill )
	steamID = play:SteamID()
	unique_id = sql.QueryValue("SELECT unique_id FROM player_skills WHERE unique_id = '"..steamID.."'")
	local value = sql.QueryValue("SELECT '"..skill.."' FROM player_skills WHERE unique_id = '"..steamID.."'")
	play:SetNWString("unique_id", unique_id)
	ply:ChatPrint( "Retreaved" )
	return value
end

function sql_value_inv( play )
	steamID = play:SteamID()
	unique_id = sql.QueryValue("SELECT unique_id FROM player_Invitory WHERE unique_id = '"..steamID.."'")
	local value = sql.QueryValue("SELECT 'invitory' FROM player_Invitory WHERE unique_id = '"..steamID.."'")
	play:SetNWString("unique_id", unique_id)
	ply:ChatPrint( "Retreaved" )
	return value
end
--Saveing
function savePlayer( play, skill, value1, stat, value2, inv )
	unique_id = play:GetNWString ("SteamID")
	local invitory = glon.encode( inv )
	sql.Query("UPDATE player_skills SET '"..skill.."' = '"..value1.."' WHERE unique_id = '"..unique_id.."'")
	sql.Query("UPDATE player_info SET '"..stat.."' = '"..value2.."' WHERE unique_id = '"..unique_id.."'")
	sql.Query("UPDATE player_Invitory SET 'invitory' = '"..glon.encode( invitory ).."' WHERE unique_id = '"..unique_id.."'")
	play:ChatPrint("Saved!")
end


--Junk that you will regret BADLY!!!!!! if you change, never use the functions under here NEVER!
function tables_exist()
 
	if (sql.TableExists("player_info") && sql.TableExists("player_skills")) then
		Msg("Both tables exist \n") --Debuging
	else
		if (!sql.TableExists("player_info")) then
			query = "CREATE TABLE player_info ( unique_id varchar(255), money int )"
			result = sql.Query(query)
			if (sql.TableExists("player_info")) then
				Msg("Succes ! table 1 created \n") --Debuging
			else --Debuging
				Msg("Somthing went wrong with the player_info query ! \n") --Debuging
				Msg( sql.LastError( result ) .. "\n" ) --Debuging
			end --Debuging
		end
		if (!sql.TableExists("player_skills")) then
			query = "CREATE TABLE player_skills ( unique_id varchar(255), Gathering int, Tracking int, Engineering int, Farming int )"
			result = sql.Query(query)
			if (sql.TableExists("player_skills")) then
				Msg("Succes ! table 2 created \n") --Debuging
			else --Debuging
				Msg("Somthing went wrong with the player_skills query ! \n") --Debuging
				Msg( sql.LastError( result ) .. "\n" ) --Debuging
			end	--Debuging
		end
		if (!sql.TableExists("player_Invitory")) then
			query = "CREATE TABLE player_Invitory ( unique_id varchar(255), invitory str )"
			local InvitoryArray = {}
			InvitoryArray[1] = "models/props_c17/oildrum001_explosive.mdl"
			local Invitory = glon.encode( InvitoryArray )
			result = sql.Query(query)
			if (sql.TableExists("player_Invitory")) then
				Msg("Succes ! table 3 created \n") --Debuging
			else --Debuging
				Msg("Somthing went wrong with the player_invitory query ! \n") --Debuging
				Msg( sql.LastError( result ) .. "\n" ) --Debuging
			end	--Debuging
		end
	end
 
end
 
function new_player( SteamID, ply )
 
		steamID = SteamID
		sql.Query( "INSERT INTO player_info (`unique_id`, `money`)VALUES ('"..steamID.."', '100')" )
		result = sql.Query( "SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'" )
		if (result) then
 
			sql.Query( "INSERT INTO player_skills (`unique_id`, `Gathering`, `Tracking`, `Engineering`, `Farming`)VALUES ('"..steamID.."', '1', '1', '1', '1')" )
			result = sql.Query( "SELECT unique_id, Gathering, Tracking, Engineering, Farming FROM player_skills WHERE unique_id = '"..steamID.."'" )
			if (result) then
				Msg("Player account created !\n")
				
				sql.Query( "INSERT INTO player_Invitory (`unique_id`, `invitory`)VALUES ('"..steamID.."', 'blank')" ) -- "" is reserved for the Glon How to play Book
				result = sql.Query( "SELECT unique_id, invitory FROM player_Invitory WHERE unique_id = '"..steamID.."'" )
				if (result) then
					Msg("Player invitory created !\n")
				else
					Msg("Something went wrong with creating a players invitory !\n")
				end
			else
				Msg("Something went wrong with creating a players skills !\n")
			end
 
		else
			Msg("Something went wrong with creating a players info !\n")
		end
end
 
function player_exists( ply )
 
	steamID = ply:GetNWString("SteamID")
 
	result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
	if (result) then
		print( "Player Retreaved\n")
	else
		new_player( steamID, ply )
	end
end
 
function Initialize()
	tables_exist()
end
 
function PlayerInitialSpawn( ply )
 
	timer.Create("Steam_id_delay", 1, 1, function()
		SteamID = ply:SteamID()
		ply:SetNWString("SteamID", SteamID)
		player_exists( ply ) 
	end);
 
end
 
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
hook.Add( "Initialize", "Initialize", Initialize )
